<?php
namespace Model;

use Model\Factory\TableGatewayFactory;

class Template extends AbstractModel
{
    public $id;
    public $description;
    public $file_name;
    public $thumb_image;
    public $name;
    public $created_date;
    public $created_by;
    public $active;


    public function getAllBlocks(){
        $blockInstanceTable = TableGatewayFactory::getBlockInstanceTable();
        $select = $blockInstanceTable->getDbSqlSelect();
        $select->join(array("tp"=>"template_block"), 'tp.block_id = block_instance.id',
        array("div_id"=>"div_id"))
        ->where(array('template_id'=>$this->id));
        $allBlock = $blockInstanceTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($allBlock as $aBlock) {
            array_push($resultArr, $aBlock);
        }
        return $resultArr;
    }

    public function addBlock($divId, $blockId){
        $templateBlockTable = TableGatewayFactory::getTemplateBlockTable();
        return $templateBlockTable->insert(array('template_id'=>$this->id,
        'block_id'=>$blockId, 'div_id'=>$divId));
    }

    public function removeAllBlock(){
        $templateBlockTable = TableGatewayFactory::getTemplateBlockTable();
        $templateBlockTable->getTableGateway()->delete('template_id = '.$this->id);

    }


    public function getAllPages()
    {
        $pageTable = TableGatewayFactory::getPageTable();
        $select = $pageTable->getDbSqlSelect();
        $select->where(array('template_id'=>$this->id));
        return $pageTable->getTableGateway()->selectWith($select);
    }


}
